package com.seveneight.baijiesteward.ui.user.presenter;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.seveneight.baijiesteward.base.MessageEvent;
import com.seveneight.baijiesteward.bean.UserBean;
import com.seveneight.baijiesteward.http.MyCallBack;
import com.seveneight.baijiesteward.ui.user.contract.IdentifyContract;
import com.seveneight.baijiesteward.ui.user.model.IdentifyModel;
import com.seveneight.baijiesteward.utils.BusUitls;
import com.seveneight.baijiesteward.utils.PreferencesUtil;

import org.greenrobot.eventbus.EventBus;

import okhttp3.RequestBody;

/**
 * Created by user1 on 2018/2/24.
 */

public class IdentifyPresenter implements IdentifyContract.IdentifyPresenter {
    IdentifyContract.IdentifyView identifyView;
    IdentifyContract.IdentifyModel identifyModel;

    public IdentifyPresenter(IdentifyContract.IdentifyView identifyView) {
        this.identifyView = identifyView;
        identifyModel = new IdentifyModel();
    }

    @Override
    public void UploadImg(Context context,RequestBody body) {
        identifyModel.UploadImg(context,
                body, new MyCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                identifyView.reBackUrl(s);
            }

            @Override
            public void onFailed() {

            }
        });
    }

    @Override
    public void startIdentify(Context context,RequestBody body) {
        identifyModel.startIdentify(context,body,
                new MyCallBack<UserBean>() {
                    @Override
                    public void onSuccess(UserBean userBean) {
                        PreferencesUtil.put(context, PreferencesUtil.userinfo, JSON.toJSONString(userBean));
                        MessageEvent event=new MessageEvent(110);
                        event.setMessage(BusUitls.LOGINSUCCESS);
                        EventBus.getDefault().post(event);
                        identifyView.identifySuccess(userBean);
                    }

                    @Override
                    public void onFailed() {

                    }
                });
    }
}
